package com.zhanvie.thread;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

public class ExtendRejectHandler implements RejectedExecutionHandler {

    public static Logger logger = LogManager.getLogger(Main1.class);

    private final LinkedBlockingQueue<Runnable> runnableTaskQueue;

    public ExtendRejectHandler() {
        runnableTaskQueue = new LinkedBlockingQueue<>();
    }

    public ExtendRejectHandler(int queueSize) {
        runnableTaskQueue = new LinkedBlockingQueue<>(queueSize);
    }

    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        try {
            runnableTaskQueue.put(r);
        } catch (InterruptedException e) {
            logger.error("rejectedExecution: task has been over or some thing error when put in tasklist");
        }
    }
}
